var app = require('../app'),
    formMod = require('../../modules/form'),
    ajaxLock = true;

toastr.options.timeOut = 2000;
toastr.options.showDuration = 0;

// 定期 表单验证
formMod.listen("/user/editPassword",{
    ajaxBefore: function(){
        ajaxLock = false;
    },
    //验证错误
    validError:function(validResutl){

        var item = validResutl.element,
            item_span = item.parents('.form-group').find('.error-tips');

        if(item.attr('name')=='user[originalPassword]'){
            if(validResutl.valid == 'notempty'){
                item_span.html('请填写原密码');
            }
        }
        if(item.attr('name')=='user[newPassword]'){
            if(validResutl.valid == 'notempty'){
                item_span.html('请输入新密码');
            }else if(validResutl.valid == 'len'){
                item_span.html('密码至少要8位哦');
            }else if(validResutl.valid == 'pwd'){
                item_span.html('密码至少要包含 数字/字母/符号 中的两种');
            }
        }
        if(item.attr('name')=='user[newPassword2]'){
            if(validResutl.valid == 'notempty'){
                item_span.html('请再次输入新密码');
            }else if(validResutl.valid == 'len'){
                item_span.html('密码至少要8位哦');
            }else if(validResutl.valid == 'pwd'){
                item_span.html('密码至少要包含 数字/字母/符号 中的两种');
            }else if(validResutl.valid == 'equal'){
                item_span.html('两次输入的密码不同，请重新确认');
            }
        }
        
    },
    //操作中清除提示
    cleanup:function(item){
        $('.error-tips').html("");
    },
    success:function(result){
        var res = result.data;
        ajaxLock = true;
        if(res.code == 200){
            toastr.success('提交数据成功');

            // 回到首页
            setTimeout(function(){
                window.location.replace("/");
            }, 3000);
        }else{
            toastr.error(res.message);
        }
    },
    error:function(){
        ajaxLock = true;
        toastr.error("网络错误，请稍候再试");
    }
});

toastr.options.onShown = function() { 
    ajaxLock = false;
    console.log(ajaxLock);
}
toastr.options.onHidden = function() { 
    ajaxLock = true;
    console.log(ajaxLock);
}
$("body")
    .on("click", ".submit-btn", function(){
        return ajaxLock;
    })
